package com.lianniu.web.tea.no.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.web.tea.no.dao.TeaNoDao;
import com.lianniu.web.tea.no.model.TeaNo;
import com.lianniu.web.tea.no.service.ITeaNoService;

@Service
@Transactional
public class TeaNoServiceImpl implements ITeaNoService {
	
	@Autowired
	private TeaNoDao teaNoDao;

	@Override
	public TeaNo selectByPrimaryKey(String teaFix) {
		// TODO Auto-generated method stub
		return teaNoDao.selectByPrimaryKey(teaFix);
	}

	@Override
	public int insert(TeaNo record) {
		// TODO Auto-generated method stub
		return teaNoDao.insert(record);
	}

	@Override
	public int updateByPrimaryKey(TeaNo record) {
		// TODO Auto-generated method stub
		return teaNoDao.updateByPrimaryKey(record);
	}

	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public int getTeaNo(String teaFix) {
		TeaNo teaNo = teaNoDao.selectByPrimaryKey(teaFix);
		if(teaNo==null){
			//没有对应的，返回1，并且插入库中1
			TeaNo record= new TeaNo();
			record.setTeaFix(teaFix);
			record.setTeaNo(1);
			teaNoDao.insert(record);
			return 1;
		}else{
			Integer no = teaNo.getTeaNo();
			teaNo.setTeaNo(no+1);
			teaNoDao.updateByPrimaryKey(teaNo);
			return no;
		}
	}
	
	
	
	
}
